﻿using System.Xml;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;

namespace GlobalBase
{
   public class Global : CatchException
   {
       private static Global _instance = null;
       public static Global Instance
       {
           get
           {
               if (null == _instance)
               {
                   _instance = new Global();
               }
               return _instance;
           }
       }
       /// <summary>
       /// 返回根据日期得到的文件名
       /// </summary>
       /// <returns></returns>
       public string GetFileName()
       {
           string fileName = DateTime.Now.Year.ToString().Trim();
           fileName += DateTime.Now.Month.ToString().Trim();
           fileName += DateTime.Now.Day.ToString().Trim();
           fileName += "_" + DateTime.Now.Hour.ToString().Trim();
           fileName += DateTime.Now.Minute.ToString().Trim();
           fileName += DateTime.Now.Second.ToString().Trim() + ".mp4";
           return fileName;
       }
       /// <summary>
       /// 得到Video工作目录
       /// </summary>
       /// <returns></returns>
       public string GetVideoDir()
       {
           string dir = System.Environment.CurrentDirectory;
           dir = dir + "\\video\\";
           if (!System.IO.Directory.Exists(dir))
           {
               System.IO.Directory.CreateDirectory(dir);
           }
           return dir;
       }
       #region 配置参数
       public string GetConfigParaValue(string key)
       {
           try
           {
               string path = System.AppDomain.CurrentDomain.BaseDirectory;
               _path = path;
               path = path + ConfigXML;
               if (!File.Exists(path))
               {
                   ShowMessage("没有发现配置文件！");
                   return null;
               }
               XmlDocument xmlDoc = new XmlDocument();
               xmlDoc.Load(path);
               XmlNodeList keyNodes = xmlDoc.SelectNodes("configuration/appSettings/add");
               string strKey = null;
               string strValue = null;
               foreach (XmlNode xmlNode in keyNodes)
               {
                   strKey = xmlNode.Attributes["key"].Value;
                   if (key.Equals(strKey))
                   {
                       strValue = xmlNode.Attributes["value"].Value;
                       break;
                   }
               }
               return strValue;
           }
           catch (Exception ex)
           {
               throw new Exception("配置文件出错！");
           }
       }
       #endregion
       public bool IsIP(string ip)
       {
           //判断是否为IP
           return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
       }

       /// <summary>
       /// 是否数字
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public bool IsNumeric(string str)
       {
           System.Text.RegularExpressions.Regex reg1
               = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
           return reg1.IsMatch(str);
       }
     
       /// <summary>
       /// 返回两个byte类型随机数
       /// </summary>
       /// <returns></returns>
       public byte[] GetRandom()
       {
           try
           {
               byte[] result = new byte[2];
               Random rand1 = new Random(DateTime.Now.Millisecond);
               Random rand2 = new Random(unchecked((int)DateTime.Now.Ticks));
               result[0] = Convert.ToByte(rand1.Next(0, 256));
               result[1] = Convert.ToByte(rand2.Next(0, 256));
               return result;
           }
           catch (Exception ex)
           {
               DealWithException(ex);
               return null;
           }
       }
       /// <summary>
       /// 释放Com 资源
       /// </summary>
       /// <param name="obj"></param>
       public void ReleaseComObject(object obj)
       {
           try
           {
               if (null != obj)
               {
                   System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
               }
           }
           catch (Exception ex)
           {
               DealWithException(ex);
           }
           finally
           {
               obj = null;
           }
       }
       #region 信息窗口
       /// <summary>
       /// 信息窗口
       /// </summary>
       /// <param name="info"></param>
       /// <param name="title"></param>
       /// <param name="enmuBtn"></param>
       /// <param name="enmuIcon"></param>
       public void ShowMessage(string info, string title, MessageBoxButtons enmuBtn, MessageBoxIcon enmuIcon)
       {
           MessageBox.Show(info, title, enmuBtn, enmuIcon);
       }
       public void ShowMessage(string info, string title)
       {
           ShowMessage(info, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
       }
       public void ShowMessage(string info)
       {
           ShowMessage(info, "信息");
       }
       #endregion
       #region 全局变量
       public readonly string ConfigXML = "CY.Excel.AddIn.dll.config";
       private string _path = null;
       #endregion
   }
}
